<html>
<head><meta charset="utf-8"><title>Does Command::env export the variable? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html">Does Command::env export the variable?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211861148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/211861148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#211861148">(Oct 01 2020 at 01:53)</a>:</h4>
<p>Does <a href="https://doc.rust-lang.org/std/process/struct.Command.html#method.env">https://doc.rust-lang.org/std/process/struct.Command.html#method.env</a> preserve the env variable across subprocesses? If not, is there a way to do that?</p>



<a name="211864120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/211864120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#211864120">(Oct 01 2020 at 02:54)</a>:</h4>
<p>When you say "subprocesses" you mean children of the process that calls <code>Command::env()</code>?</p>



<a name="211864818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/211864818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#211864818">(Oct 01 2020 at 03:08)</a>:</h4>
<p>I mean grandchildren</p>



<a name="211864827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/211864827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#211864827">(Oct 01 2020 at 03:08)</a>:</h4>
<p>but the answer is yes because <code>SYSROOT</code> is being passed through, I figured it out in <a href="#narrow/stream/246057-t-cargo/topic/Does.20cargo.20pass.20--sysroot.3F">https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Does.20cargo.20pass.20--sysroot.3F</a></p>



<a name="212219172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212219172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212219172">(Oct 04 2020 at 11:32)</a>:</h4>
<p>I don't think there is any way <em>not</em> to pass variables to subprocesses</p>



<a name="212219221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212219221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212219221">(Oct 04 2020 at 11:34)</a>:</h4>
<p>this is probably confusion caused by <code>export</code> in shell scripts, which is a horrible name... linux shells have variables and and environment and they are closely interacting; <code>export</code> is how you move a variable from being a normal local shell variable to being an env var.<br>
but env vars are always propagated to chilren and grandchildren.</p>



<a name="212223126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212223126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212223126">(Oct 04 2020 at 13:21)</a>:</h4>
<p>ahhh</p>



<a name="212223129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212223129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212223129">(Oct 04 2020 at 13:21)</a>:</h4>
<p>yeah that makes a lot of sense, thank you!</p>



<a name="212223556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212223556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212223556">(Oct 04 2020 at 13:33)</a>:</h4>
<p>isn't that OS depends ?</p>



<a name="212223648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212223648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212223648">(Oct 04 2020 at 13:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209117">lzutao</span> <a href="#narrow/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F/near/212223556">said</a>:</p>
<blockquote>
<p>isn't that OS depends ?</p>
</blockquote>
<p>what's "that"? env vars are AFAIK inherited to (grand)children on all OSes. I would say things that do not behave like that should not be called "env vars" as that is one of their primary features.</p>



<a name="212224222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212224222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212224222">(Oct 04 2020 at 13:51)</a>:</h4>
<p>because there are no standard defining that sub-process or child process must inherent all env variable from its parent process.<br>
But Windows and posix does that, which are what we care the most.<br>
So just don't care about that, as nobody else care either.</p>



<a name="212224874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212224874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212224874">(Oct 04 2020 at 14:09)</a>:</h4>
<p>If children do not inherit some kind of "variables", calling them "environment variables" would be a misnomer. I would describe such platforms as "not supporting env vars".</p>



<a name="212226940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212226940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212226940">(Oct 04 2020 at 15:06)</a>:</h4>
<p>IIRC, technically Windows doesn't truly have a concept of child processes. Processes are just processes that may or may not share some resource or another (e.g. they just so happen to be attached to the same console). That said, the default for <a href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw"><code>CreateProcess</code></a> is to copy/paste the environment block of the "parent" process unless you explicitly provide another environment block.</p>
<blockquote>
<p><code>lpEnvironment</code></p>
<p>A pointer to the environment block for the new process. If this parameter is NULL, the new process uses the environment of the calling process.</p>
</blockquote>



<a name="212229196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212229196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212229196">(Oct 04 2020 at 16:07)</a>:</h4>
<p>Just like you can set a different environment with <code>execve</code></p>



<a name="212240533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212240533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212240533">(Oct 04 2020 at 20:35)</a>:</h4>
<p><span class="user-mention" data-user-id="260325">@Chris Denton</span> "the process that spawned the current one" seems like a close enough notion of "child process" for the purpose of this definition -- there is no need to keep that state around.</p>



<a name="212240555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Does%20Command%3A%3Aenv%20export%20the%20variable%3F/near/212240555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chris Denton <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Does.20Command.3A.3Aenv.20export.20the.20variable.3F.html#212240555">(Oct 04 2020 at 20:35)</a>:</h4>
<p>Fair enough.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>